﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.IO;

using CaseStudy.Web.UserControl;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;


namespace CaseStudy.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        private string displayCount = System.Configuration.ConfigurationManager.AppSettings["LandingDisplayCount"].ToString();
        private string cnSiteRoot = System.Configuration.ConfigurationManager.AppSettings["CNSiteRoot"].ToString();
        private string imageFolder = System.Configuration.ConfigurationManager.AppSettings["ImageFolderName"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

                string query = string.Empty;
                if (Request.QueryString["view"] != null)
                    query = Request.QueryString["view"].ToLower();

                if (query.Equals("all"))
                {
                    linkMore.Text = "显示前" + displayCount + "条信息";
                    linkMore.NavigateUrl = "Default.aspx";
                }
                else
                {
                    linkMore.Text = "更多";
                }
            }
        }

        private void BindData()
        {
            DataSet dataSet = CaseStudyBLL.GetAll();
            if (dataSet.Tables.Count == 0) return;

            DataTable table = dataSet.Tables[0];
            int total = dataSet.Tables[0].Rows.Count;

            int count = 20;
            int temp = -1;
            int.TryParse(displayCount, out temp); 
            
            string query = string.Empty;
            if (Request.QueryString["view"] != null)
                query = Request.QueryString["view"].ToLower();

            if (query.Equals("all"))
            {
                count = total;
            }
            else
            {
                count = temp > total ? total : temp;
            }

            if (count == 0) linkMore.Visible = false; 

            TableRow row = new TableRow();
            TableCell cell;

            for (int i = 0; i < count; i++)
            {
                string id = table.Rows[i]["Id"].ToString();
                string title = HttpUtility.HtmlDecode(table.Rows[i]["Title"].ToString());
                string client = string.Empty;
                ClientInfo tempInfo = ClientBLL.GetInfo(table.Rows[i]["Client"].ToString());
                if (tempInfo != null)
                {
                    client = HttpUtility.HtmlDecode(tempInfo.Value);
                }
                string imgName = table.Rows[i]["Image"].ToString();

                string imageAddress = "images/default.png";
                if (!string.IsNullOrEmpty(imgName))
                {
                    imageAddress = cnSiteRoot + "/" + imageFolder + "/" + imgName;
                }

                CaseStudyItem item = new CaseStudyItem(id, title, client, imageAddress);

                cell = new TableCell();

                cell.Controls.Add(item);
                cell.CssClass = "LandingCell";
                row.Cells.Add(cell);

                if (row.Cells.Count == 4)
                {
                    tblCaseStudies.Rows.Add(row);
                    row = new TableRow();
                    continue;
                }

                if ((count - i) == 1)
                {
                    tblCaseStudies.Rows.Add(row);
                }
            }
        }
    }
}
